package com.learner.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.learner.blog.mapper.MainBlogMapper;
import com.learner.blog.repository.BlogColectionRespository;
import com.learner.blog.repository.CategoryRepository;
import com.learner.blog.repository.MainBlogRepository;
import com.learner.blog.service.MainBlogService;
import com.learner.common.exception.ExceptionUtil;
import com.learner.common.exception.RestException;
import com.learner.common.page.PageHelp;
import com.learner.pojo.blog.Blog;
import com.learner.pojo.blog.BlogType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ice2020x
 * @Date: 2023/2/23
 * @Description: 博客
 **/
@Service
@Slf4j
public class MainBlogServiceImpl extends ServiceImpl<MainBlogMapper, Blog> implements MainBlogService {

    @Autowired
    private MainBlogRepository mainBlogRepository;

    @Autowired
    private MainBlogMapper mainBlogMapper;

    @Autowired
    private BlogColectionRespository blogColectionRespository;

    @Autowired
    private CategoryRepository categoryRepository;


    @Override
    public Blog getOneBlog(String id) {
        Blog blog = mainBlogMapper.selectById(id);
        if (blog == null) {
            throw ExceptionUtil.createException(RestException.class, 4000002);
        }
        return blog;
    }


    @Override
    public void deleteBlogById(String id) {
        log.info("根据id删除博客信息，id为:{}", id);
        mainBlogRepository.deleteById(id);
    }


    @Override
    public List<BlogType> findCateList() {
        return categoryRepository.findAllCateList();
    }

    @Override
    public Blog addBlog(Blog blog) {
        String content = blog.getContent();
        String posterId = blog.getPosterId();
        String title = blog.getTitle();
        if (StringUtils.isBlank(content) || StringUtils.isBlank(posterId) || StringUtils.isBlank(title)) {
            throw ExceptionUtil.createException(RestException.class, 4000001);
        }
        blog.setCommentCount(0);
        blog.setVoteUp(0);
        blog.setVoteDown(0);
        blog.setViewCount(0);
        if (blog.getRecommend() == null) {
            blog.setRecommend(0);
        }
        if (blog.getLevel() == null) {
            blog.setLevel(0);
        }
        if (StringUtils.isBlank(blog.getEditMode())) {
            blog.setEditMode("富文本编辑器");
        }
        blog.setStatus(0);
        blog = mainBlogRepository.save(blog);
        return blog;
    }


    @Override
    public IPage<Blog> getPageBlog(PageHelp pageHelp) {
        Integer page = pageHelp.getPage();
        Integer size = pageHelp.getSize();
        String property = pageHelp.getProperty();
        //默认第一页，一页6条数据
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 6;
        }
        IPage<Blog> blogPage = new Page<>(page, size);

        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();

        String keys = pageHelp.getKeys();
        if (StringUtils.isNotBlank(keys)) {
            queryWrapper.and(criteriaWrapper -> criteriaWrapper
                    .like("view_count", keys)
                    .or()
                    .like("title", keys));
        }
        String userId = pageHelp.getUserId();
        // 如果存在userId 则查询的是我发布的博客
        if (StringUtils.isNotBlank(userId)) {
            queryWrapper.eq("poster_id", userId);
        }


        //  默认创造时间降序排列
        if (StringUtils.isBlank(property)) {
            queryWrapper.orderByDesc("create_time");
        } else {
            queryWrapper.orderByAsc(property);
        }

        return mainBlogMapper.selectPage(blogPage, queryWrapper);
    }


}
